<?php   // $Id: fact_lister.php 288 2008-09-20 09:35:54Z pcoustillas $ ?>
<?php
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx 
Modification Pierre Coustillas */
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");?>

<script type="text/javascript" src="javascripts/confdel.js"></script>
<script type="text/javascript" src="javascripts/tri_formulaire.js"></script>


<table class="page" align="center">
	<tr>
		<td class="page" align="center">
		<?php
		require_once("include/head.php");
		?>
		</td>
	</tr>
	<tr>
		<td class="page" align="center">
			<?php
			if ($user_fact == "n") { 
				echo "<h1>$lang_facture_droit</h1>";
				exit;
			}

			if (isset($msg_validation)) //provient de la page fact_validation ou fact_devalidation qui un include de cette page
			{
				echo "<span class='grand'>$msg_validation</span>";
			}

			if (isset($_GET['message']))
			{
				echo "<h1>" . stripslashes($_GET['message']) . "</h1>";
			}

			$sql = "SELECT mail, login, date_fact,
						total_fact_ttc,total_fact_h, payement, num_client, num, nom, valide 
						FROM ${tblpref}facture RIGHT JOIN ${tblpref}client on client = num_client";


			// ----------  DEBUT DE GESTION DES FILTRES ---------------------
			if (isset($_GET['annuler_filtre'])) //vidage des filtres si demandé
			{
				$_SESSION['filtre_fact_client'] = "";
				$_SESSION['filtre_fact_num'] = "";
				$_SESSION['filtre_fact_date'] = "";
				$_SESSION['filtre_fact_HT'] = "";
				$_SESSION['filtre_fact_TTC'] = "";
				$_SESSION['filtre_regler'] = "";
				$_SESSION['filtre_non_regler'] = "1";
			}

			$filtre=" WHERE num > 0 AND ";

			if (isset($_GET['filtrer']))
			{
				// MAJ des variables de session de filtre si il y a eu demande de filtrage
				if (isset($_GET['filtre_fact_client']) && $_GET['filtre_fact_client']<>0 )
					$_SESSION['filtre_fact_client']=$_GET['filtre_fact_client'];
				if (isset($_GET['filtre_fact_client']) && $_GET['filtre_fact_client']==0 )
					unset($_SESSION['filtre_fact_client']);
				if (isset($_GET['filtre_fact_num']))
					$_SESSION['filtre_fact_num']=$_GET['filtre_fact_num'];
				if (isset($_GET['filtre_fact_date']))
					$_SESSION['filtre_fact_date']=$_GET['filtre_fact_date'];
				if (isset($_GET['filtre_fact_HT']))
					$_SESSION['filtre_fact_HT']=$_GET['filtre_fact_HT'];
				if (isset($_GET['filtre_fact_TTC']))
					$_SESSION['filtre_fact_TTC']=$_GET['filtre_fact_TTC'];
				if (isset($_GET['filtre_regler']))
				{
					$_SESSION['filtre_regler']=$_GET['filtre_regler'];
				} else {
					$_SESSION['filtre_regler']="";
				}
				if (isset($_GET['filtre_non_regler']))
				{
					$_SESSION['filtre_non_regler']=$_GET['filtre_non_regler'];
				} else {
					$_SESSION['filtre_non_regler']="";
				}
			} 
			
			//if ( ! isset($_GET['annuler_filtre']) && ! isset($_GET['filtrer']) && ! isset($_SESSION['filtre_bon_date']))


			//Création de la requete. Ne se trouve pas dans le test "Si frm poster" car si le user revient le filtre doit tj etre en place
			if (isset($_SESSION['filtre_fact_client']) && $_SESSION['filtre_fact_client'] != "" )
			{
				$filtre .= "client="  . $_SESSION['filtre_fact_client'] . " AND ";
			}
			if (isset($_SESSION['filtre_fact_num']) && $_SESSION['filtre_fact_num'] != "")
			{
				$filtre .= "num" . saisie_to_sql($_SESSION['filtre_fact_num'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_fact_date']) && $_SESSION['filtre_fact_date'] != "")
			{
				$filtre .= "date_fact" . saisie_to_sql($_SESSION['filtre_fact_date'],"date") . " AND ";
			}
			if (isset($_SESSION['filtre_fact_HT']) &&$_SESSION['filtre_fact_HT'] != "")
			{
				$filtre .= "total_fact_h" . saisie_to_sql($_SESSION['filtre_fact_HT'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_fact_TTC']) && $_SESSION['filtre_fact_TTC'] != "")
			{
				$filtre .= "total_fact_ttc" . saisie_to_sql($_SESSION['filtre_fact_TTC'],"num") . " AND ";
			}
			if (!isset($_SESSION['filtre_regler']) || $_SESSION['filtre_regler'] == "")
			{
				$filtre .= "payement = 'non' AND ";
			}
			if (!isset($_SESSION['filtre_non_regler']) || $_SESSION['filtre_non_regler'] == "")
			{
				$filtre .= "payement != 'non' AND ";
			}

			$filtre = substr($filtre,0,strlen($filtre) -5); //retirer " AND " de la fin du filtre
			$sql .= " $filtre ";


			
			//echo $sql;

			// ----------------  FIN DE GESTION DES FILTRES  -----------------------------

	
			$tri_ordre=isset($_GET['tri_ordre'])?$_GET['tri_ordre']:"DESC"; //ordre de tri
			$tri_champ=isset($_GET['tri_champ'])?$_GET['tri_champ']:"num"; // champ de tri

         $sql .= " ORDER BY " . $tri_champ . " " . $tri_ordre;

			$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());
			?>
			<center>

				<?php //affichage des 2 tableaux du hauts ?>
				<table class="boiteaction" width="80%">
					<tr>
						<td width="45%">
							<?php bouton_aide("fact") ?>
						</td>
						<td width="5%">&nbsp;</td>
						<td width="50%">
							<?php //tableau de filtre ?>
							<form action="fact_lister.php" method="get" name="frm_fact">
								<input type="hidden" name="tri_champ" value="<?php echo $tri_champ ?>" />
								<input type="hidden" name="tri_ordre" value="<?php echo $tri_ordre ?>" />
								<input type="hidden" name="filtrer"/>
								<table width="90%">
									<caption><?php aff_lang("Filtre") ?></caption>
									<tr>
										<td class="texte0" width="20%"><?php aff_lang("Facture n°") ?></td>
										<td class="texte0" width="30%">
											<input type="text" name="filtre_fact_num" value="<?php echo isset($_SESSION['filtre_fact_num'])?$_SESSION['filtre_fact_num']:"" ?>" />
										</td>
										<td class="submit" rowspan="7" width="20%">
											<br/><br/>
											<input type="submit" value="Filtrer !"/>
											<br/><br/>
											<a href="fact_lister.php?annuler_filtre=oui"><img border="0" src="image/delete.png" alt="<?php echo $lang_supprimer; ?>" /></a>
											<br/><br/>
											<?php bouton_aide("filtre") ?>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Client") ?></td>
										<td class="texte0">
											<?php 
												require_once("include/choix_cli.php");
												inclure_combo_client("filtre_fact_client",isset($_SESSION['filtre_fact_client'])?$_SESSION['filtre_fact_client']:"","frm_fact") ?>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Date") ?></td>
										<td class="texte0">
											<input type="text" size="14" name="filtre_fact_date" value="<?php echo isset($_SESSION['filtre_fact_date'])?$_SESSION['filtre_fact_date']:"" ?>" />
											<a href="#" onclick="window.open('include/pop.calendrier.php?frm=frm_fact&amp;ch=filtre_fact_date','calendrier','width=415,height=190,left=200,top=200,scrollbars=0').focus();">
												<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
											</a>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_h_tva; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_fact_HT" value="<?php echo isset($_SESSION['filtre_fact_HT'])?$_SESSION['filtre_fact_HT']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_ttc; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_fact_TTC" value="<?php echo isset($_SESSION['filtre_fact_TTC'])?$_SESSION['filtre_fact_TTC']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0" colspan=2>
											<?php aff_lang("Réglée") ?> 
													<input type="checkbox" value="1" name="filtre_regler" <?php if (isset($_SESSION['filtre_regler']) && $_SESSION['filtre_regler'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
											<?php aff_lang("Non réglée") ?>
													<input type="checkbox" value="1" name="filtre_non_regler" <?php if (isset($_SESSION['filtre_non_regler']) && $_SESSION['filtre_non_regler'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
										</td>
									</tr>
								</table>
							</form>

						</td>
					</tr>
				</table>
				<br/>



			<table class="boiteaction">
				<tr>
					<th width="30"><a href="javascript:ecrit_ordre_et_post('frm_fact','num')">N°</a></th>
					<th width="180"><a href="javascript:ecrit_ordre_et_post('frm_fact','nom')"><?php echo $lang_client; ?></a></th>
					<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_fact','date_fact')"><?php echo $lang_date; ?></a></th>
					<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_fact','total_fact_h')"><?php  aff_lang("Total HT") ?></a></th>
					<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_fact','total_fact_ttc')"><?php  aff_lang("Total TTC")  ?></a></th>
					<th><a href="javascript:ecrit_ordre_et_post('frm_fact','payement')"><?php echo $lang_pay; ?></a></th>
					<th colspan="7"><?php echo $lang_action; ?> &nbsp; <?php bouton_aide("fact_icones") ?></th>
				</tr>
				<?php
				$nombre=1;
				$total_ht=0;
				$total_ttc=0;
				while($data = mysql_fetch_array($req))
				{
					$client = $data['nom'];
					$client = htmlentities($client, ENT_QUOTES,'UTF-8');
					$payement = $data['payement'];
					$nombre = $nombre +1;
					if($nombre & 1){
						$line="0";
					} else {
						$line="1";
					}

					$num = $data['num'];
					$num_client =$data['num_client'];
					$total = $data['total_fact_ttc'];
					$date_fact = sql_to_date($data['date_fact']);
					$mail = $data['mail'];
					$login = $data['login'];
					$valide= $data['valide'];
					$total_fact_h = $data['total_fact_h'];

					$total_ht = $total_ht + $total_fact_h;
					$total_ttc = $total_ttc + $total;


					?>
				<tr class="texte<?php echo "$line" ?>" onmouseover="this.className='highlight'" onmouseout="this.className='texte<?php   echo"$line" ?>'">
						<td class="highlight"><?php echo $num; ?></td>
						<td class="highlight"><?php echo $client; ?></td>
						<td class="highlight"><?php echo $date_fact; ?></td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier($total_fact_h); ?>&nbsp;</td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier($total); ?>&nbsp;</td>
						<td class="highlight">
						<form action="fact_change_payement.php" method="post">
							<input type="hidden" name="num_fact" value="<?php echo $num ?>" />
							<select name="payement" onchange="javascript:submit()" <?php if ($payement == "non" ) { echo "style='color:red' "; } ; if ($valide == 0 ) { echo "disabled='disabled' "; } ?> >
								<?php
									$type_payement = array ("carte","espèce","virement","chèque","non","autre");
									foreach ($type_payement as $paye)
									{
										$opt="<option value='$paye' ";
										if ($paye == $payement) { $opt.= "selected='selected'"; }
										$opt.=">$paye</opion>";
										echo $opt;
									}
								?>
							</select>
						</form>

						</td>
						<?php 
					if ($valide == 0)
					{
						?>
						<td class="highlight"><a href="fact_edit.php?num_fact=<?php echo"$num"; ?>"><img src="image/edit.png" border="0" alt="editer" /></a></td>
						<td class="highlight"><a href="fact_validation.php?num_fact=<?php echo"$num"; ?>"><img src="image/ok_kde.png" border="0" alt="Validation" /></a></td>
						<td class="highlight" colspan="4">&nbsp;</td>
						<td class="highlight"><a href="fact_delete.php?num_fact=<?php echo"$num"; ?>"><img src="image/delete.png" border="0" alt="Supprimer" /></a></td>
						<?php
					} else {
						?>
						<td class="highlight"><a href="fact_edit.php?num_fact=<?php echo"$num"; ?>"><img src="image/edit.png" border="0" alt="editer" /></a></td>
						<td class="highlight">&nbsp;</td>
						<td class="highlight"><a href="fact_devalidation.php?num_fact=<?php echo"$num"; ?>"><img src="image/undo.png" border="0" alt="Dévalider" /></a></td>
						<td class="highlight">
							<form action="fpdf/fact_pdf.php" method="post" target="_blank">
								<input type="hidden" name="client" value="<?php echo $num_client ?>" />
								<input type="hidden" name="num" value="<?php echo $num ?>" />
								<input type="hidden" name="user" value="adm" />
								<input type="image" src="image/imprimante.png" style="border: none; margin: 0;" alt="imprimer" />
							</form>
						</td>
						<?php
						if ($mail != '' and $login != '')
						{ 
							?>
							<td class="highlight">
								<a href='notifi_cli.php?type=fact&amp;mail=<?php echo "$mail"; ?>'><img src='image/mail.gif' alt='mail' border='0' onclick="return confirmDelete('<?php echo "$lang_conf_notif $client $lang_conf_notif2 $num ?"; ?>')" /></a>
							</td>
							<?php
						} else {
							?>
							<td class="highlight">&nbsp;</td>
							<?php
						}

						if ($mail != '')
						{
							?>
							<td class="highlight">
								<form action="fpdf/fact_pdf.php" method="post" onclick="return confirmDelete('<?php echo "$lang_conf_env $num $lang_conf_env2 $client ?"; ?>')">
									<input type="hidden" name="client" value="<?php echo $num_client ?>" />
									<input type="hidden" name="num" value="<?php echo $num ?>" />
									<input type="hidden" name="user" value="adm" />
									<input type="hidden" name="mail" value="y" />
									<input type="image" src="image/pdf.gif" style=" border: none; margin: 0;" alt="envoyer par mail" />
								</form>
							</td>
							<?php
						} else {
							?>
							<td class="highlight">&nbsp;</td>
							<?php
						}
						?>
						<td class="highlight">&nbsp;</td>
						<?php
					} //fin de facture Valide
					?>
				</tr>
			<?php
			}
			?>

				<tr>
					<td colspan="3">&nbsp;</td>
					<td colspan="2"> <hr/></td>
					<td colspan="8">&nbsp;</td>
				</tr>


				<tr>
					<td colspan="2">&nbsp;</td>
					<td style="text-align:center">TOTAL</td>
					<td style="text-align:right"><?php echo montant_financier($total_ht) ?>&nbsp; </td>
					<td style="text-align:right"><?php echo montant_financier($total_ttc) ?>&nbsp; </td>
					<td colspan="8">&nbsp;</td>
				</tr>

			</table>
			</center>
		</td>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>
</body>
</html>

